package com.client.elements.canvas;

import java.util.ArrayList;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.client.elements.canvas.tools.Kreis;

public class KreiseDurcheinander_Mouse {
	static Canvas canvas_main;
	static Context2d context_main;

	static ArrayList<Kreis> kreise_list = new ArrayList<Kreis>();

	final static TextBox txt = new TextBox();
	final static Button btn = new Button("Start");

	static int mouse_X = 200;
	static int mouse_Y = 200;
	
	static int anzahl = 80;

	public static Widget kreis_demo() {
		canvas_main = Canvas.createIfSupported();
		canvas_main.setCoordinateSpaceWidth(400);
		canvas_main.setCoordinateSpaceHeight(400);
		canvas_main.addStyleName("border");

		context_main = canvas_main.getContext2d();

		timer(true);
		setHandlers();
		
		btn.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (btn.getText().equals("Start")) {
					try {
						anzahl = Integer.parseInt(txt.getText());
					} catch (NumberFormatException e) {
						Window.alert("Ungültiger Wert");
						txt.setText(anzahl + "");
						return;
					}

					for (int j = 1; j < anzahl; j++) {
						int richtung_x = (int)(Math.random() * 15 + 5);
						int richtung_y = (int)(Math.random() * 15 + 5);

						Kreis k = new Kreis(1, 50, 50, richtung_x, richtung_y);

						kreise_list.add(k);
					}
					timer(false);
					btn.setText("Stop");
				}else if (btn.getText().equals("Stop")) {
					kreise_list.clear();
					timer(true);
					btn.setText("Start");
				}
			}
		});

		txt.setText(anzahl + "");

		FlowPanel main = new FlowPanel();
		main.add(canvas_main);
		main.add(txt);
		main.add(btn);

		return main;
	}

	private static void setHandlers() {
		canvas_main.addMouseMoveHandler(new MouseMoveHandler() {
			
			@Override
			public void onMouseMove(MouseMoveEvent event) {
				mouse_X = event.getRelativeX(canvas_main.getElement());
				mouse_Y = event.getRelativeY(canvas_main.getElement());
			}
		});
	}

	final static Timer timer = new Timer() {
		@Override
		public void run() {
			context_main.clearRect(0, 0, 400, 400);
			
			Kreis.draw_Mauskreis(context_main, mouse_X, mouse_Y, 80);
			
			for (int i = 0; i < kreise_list.size(); i++) {
				kreise_list.get(i).updateCirclesAroundMouse(context_main, mouse_X, mouse_Y, 80);
			}
		}
	};

	private static void timer(boolean stop) {
		if (stop == false) {
			timer.scheduleRepeating(25);		
		}else {
			timer.cancel();
		}
	}
}